package com.cn.demo.controller;

import com.cn.demo.entity.Activity;
import com.cn.demo.service.ActivityService;
import com.cn.demo.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 秒杀活动管理
 *
 * @author wangjf
 * @version 1.0
 * @createDate 2019/11/18 15:21
 * @projectName test
 */
@RestController
@RequestMapping("/activty")
public class ActivtyController {

    @Autowired
    private ActivityService activityService;

    /**
     * 添加活动
     *
     * @param activity
     * @return com.cn.demo.util.R<java.lang.String>
     * @author: wangjf
     * @createTime: 2019/11/18 15:33
     */
    @PostMapping
    public R<String> addActivty(@RequestBody Activity activity) {

        boolean result = activityService.saveActivity(activity);
        if (result) {
            return R.ok("秒杀活动创建成功!");
        } else {
            return R.failed("秒杀活动创建失败!");
        }
    }

    /**
     * 启动活动
     *
     * @param id
     * @return com.cn.demo.util.R<java.lang.String>
     * @author: wangjf
     * @createTime: 2019/11/18 15:34
     */
    @PutMapping("/start/{id}")
    public R<String> startActivty(@PathVariable("id") Long id) {

        Activity getObj = activityService.getById(id);
        if (getObj == null) {
            return R.failed("互动不存在");
        }
        boolean result = activityService.startActivity(id);
        if (result) {
            return R.ok("活动启动成功");
        }
        return R.failed("活动启动失败");
    }

    /**
     * 秒杀结束
     *
     * @param id
     * @return com.cn.demo.util.R<java.lang.String>
     * @author: wangjf
     * @createTime: 2019/11/21 15:22
     */
    @PutMapping("/end/{id}")
    public R<String> endActivty(@PathVariable("id") Long id) {

        Activity getObj = activityService.getById(id);
        if (getObj == null) {
            return R.failed("互动不存在");
        }
        boolean result = activityService.endActivity(id);
        if (result) {
            return R.ok("活动停止成功");
        }
        return R.failed("活动停止失败");
    }

    /**
     * 秒杀取消
     *
     * @param id
     * @return com.cn.demo.util.R<java.lang.String>
     * @author: wangjf
     * @createTime: 2019/11/21 15:23
     */
    @PutMapping("/cancel/{id}")
    public R<String> cancelActivty(@PathVariable("id") Long id) {

        Activity getObj = activityService.getById(id);
        if (getObj == null) {
            return R.failed("互动不存在");
        }
        boolean result = activityService.cancelActivity(id);
        if (result) {
            return R.ok("活动取消成功");
        }
        return R.failed("活动取消失败");
    }

}
